//
//  ThirdResetPasswordViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class ThirdRegisterOrResetViewController: AccountBaseViewController,InputViewDelegate {
    
    @IBOutlet weak var sureBtn: SureButton!
    @IBOutlet weak var passwordInput: InputView!
    @IBOutlet weak var secondPasswordInput: InputView!
    
    @IBOutlet weak var secondInputHeight: NSLayoutConstraint!
    
    var viewModel: RegisterOrResetViewModel?
   
    var type: Int = 0 ///0 手机注册 1 邮箱注册, 2 手机找回密码,3 邮箱找回密码
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    func initUI() {
        if type == 0 {
//            self.secondPasswordInput.isHidden = true
//            self.secondInputHeight.constant = 0
            self.title = kText("手机注册")
        }else if type == 1 {
//            self.secondPasswordInput.isHidden = true
//            self.secondInputHeight.constant = 0
            self.title = kText("邮箱注册")
        }else if type == 2 {
            self.title = kText("手机找回密码")
        }else {
            self.title = kText("邮箱找回密码")
        }
        self.passwordInput.delegate = self
        self.passwordInput.placeholder = kText("请输入新密码")
        self.passwordInput.input.isSecureTextEntry = true
        self.secondPasswordInput.delegate = self
        self.secondPasswordInput.input.isSecureTextEntry = true
        self.secondPasswordInput.placeholder = kText("请再次输入密码")
        self.sureBtn.isEnabled = false
        self.sureBtn.setTitle(kText("提交"), for: UIControlState.normal)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.passwordInput.input.becomeFirstResponder()
        initData()
    }
    func initData() {
        self.viewModel?.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_register  {
                if isSuccess {
                    if self?.type == 0 {//手机注册
                        let vc = SettingPayPasswordViewController()
                        vc.viewModel.mobile = self?.viewModel?.mobile ?? ""
                        vc.viewModel.mobileCode = self?.viewModel?.dynamicCode ?? ""
                        self?.navigationController?.pushViewController(vc, animated: true)
                    }else {
                        self?.navigationController?.popToRootViewController(animated: true)
                    }
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }else if state == ResponseState.mine_resetPwd {
                if isSuccess {
                    let login = LoginViewController()
                    self?.navigationController?.customPushViewController(login, animated: true)
                        CYMBShow.showMessage(kText("重置密码成功"))
                }else {
                    if let msg = errorMsg {
                         CYMBShow.showMessage(msg)
                    }
                }
            }
        }
    }
    @IBAction func sure(_ sender: SureButton) {
        dPrint("sure")
        if self.passwordInput.input.text == self.secondPasswordInput.input.text {
            let regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$"
            let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
            let isMatch = predicate.evaluate(with: self.passwordInput.input.text!)
            if isMatch {
                if type == 2 || type == 3 {
                    viewModel?.resetPwd(password: self.passwordInput.input.text!)
                }else {
                    viewModel?.register(password: self.passwordInput.input.text!)
                }
            }else {
                CYMBShow.showMessage(kText("请输入8-20位数字、字母组合"))
            }
        }else {
            CYMBShow.showMessage(kText("两次密码输入不一致"))
        }
    }
    
    func inputViewTextDidChange(inputView: InputView) {
        if let passwordCount = self.passwordInput.input.text?.count,   passwordCount > 0 {
            if type == 0 || type == 1 {
                self.sureBtn.isEnabled = true
            }else {
                if let sePasswordCount = self.secondPasswordInput.input.text?.count,sePasswordCount > 0 {
                    self.sureBtn.isEnabled = true
                }
            }
        }else {
            self.sureBtn.isEnabled = false
        }
    }
}
